<template>
  <yl_report_design ref="printDesigner" :temp-value="value" :widget-options="widgets" :module-options="modules" @save="handleSave"/>
</template>
<script>
import {widgetOptions, moduleOptions} from '../data/options'
import _ from "lodash";
import {getViewTypeByType, getViewDescByType, deepTrav} from "@/utils/utils";

export default {
  data() {
    return {
      index: '',
      templateId: null,
      templateName: '',
      value: {
        templateCode: 'NEW_TEMPLATE_',
        templateName: '新建打印模板',
        width: 750,
        height: 550,
        pageWidth: 100,
        pageHeight: 100,
        tempItems: [],
      },
      widgets: widgetOptions,
      modules: moduleOptions
    }
  },

  created() {
    this.templateId = Number(this.$route.query.templateId)
    try {
      this.value = this.findTemplateByTemplateId(this.templateId)
      this.widgets = widgetOptions
    } catch (err) {
      console.error(err)
    }
  },

  methods: {
    findTemplateByTemplateId(templateId) {
      const templateList = JSON.parse(localStorage.getItem('tempList')) || []
      for (let template of templateList) {
        if (template.templateId === templateId) {
          return template
        }
      }
    },
    handleSave(data) {
      data.templateId = this.templateId
      if (!data.docHeader?.type) {
        delete data.docHeader
      }
      if (!data.pageHeader?.type) {
        delete data.pageHeader
      }
      if (!data.docBody?.type) {
        delete data.docBody
      }
      if (!data.pageFooter?.type) {
        delete data.pageFooter
      }
      if (!data.docFooter?.type) {
        delete data.docFooter
      }
      deepTrav(data, (objc, key) => {
        if (typeof objc === 'object') {
          // delete objc.name
          delete objc.title
          delete objc.dragable
          delete objc.viewType
          delete objc.style
          delete objc.uuid
          delete objc.desc
          delete objc.isEdit
          delete objc.resizable
          if (objc.underline === true) {
            objc.underline = 1
          } else {
            delete objc.underline
          }

          if (objc.bold === true) {
            objc.bold = 1
          } else {
            delete objc.bold
          }

          if (objc.tilt === true) {
            objc.tilt = 1
          } else {
            delete objc.tilt
          }

          if (objc.strikethrough === true) {
            objc.strikethrough = 1
          } else {
            delete objc.strikethrough
          }
        }
      })
      window.parent.postMessage(data, '*')
      let tempList = JSON.parse(localStorage.getItem('tempList')) || []
      tempList[this.index] = data
      localStorage.setItem('tempList', JSON.stringify(tempList))
      console.log(JSON.stringify(data))
      //blob转file
      const templateFile = new File([JSON.stringify(data)], 'template2.json', {
        type: "application/json",
        lastModified: Date.now()
      });
      const url = "http://122.9.140.63:9090/uploadTemplate";
      // 在axios中，使用promise语法代替回调函数
      this.axios
          .postForm(url, {templateId: "template2", templateName: "www", templateCode: "www", template: templateFile}, {responseType: "json"})
          // 成功后执行
          .then((result, a, b, c) => {
            //result 就是请求的结果
            debugger
            console.log(result);
            // 把请求返回的数据存储到本地的data上
            this.data = result.data;
          })
          // 错误处理
          .catch((err) => {
          });
      // this.$router.back()
    },
    // 手动初始话模板数据
    initTemp() {
      this.$refs.printDesigner.initTemp(this.value, this.widgets)
    }
  },
}
</script>
